//
//  QDLineGraphView.swift
//  QDMetroClient
//
//  Created by YueWen on 2019/3/29.
//  Copyright © 2019 YueWen. All rights reserved.
//

import Foundation


/// 线网图顶部的搜索视图
final class QDLineGraphSearchView: UIImageView {
    
    /// 左侧的imageView
    let leftImageView = UIImageView()
    /// 填写文本你的文本框
    let textField = UITextField()
    /// 右侧的imageView
    let rightImageView = UIImageView()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        image = "metro_search_bg".namedImage?.resizableImage(withCapInsets: UIEdgeInsets(top: 10, left: 7, bottom: 15, right: 7), resizingMode: .stretch)
        isUserInteractionEnabled = true
        
        leftImageView.image = "metro_search".namedImage
        leftImageView.contentMode = .left
        leftImageView.ritl_width = 19 + 7
        leftImageView.ritl_height = 44
        
        textField.leftView = leftImageView
        textField.leftViewMode = .always
        textField.font = Font.regular.font(size: 16)
        textField.tintColor = QDColors.theme
        textField.textColor = QDColors.black
        textField.placeholder = "请输入查询站点的名称"
        textField.clearButtonMode = .whileEditing
        
        addSubview(textField)
        addSubview(rightImageView)
        
        textField.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(21)
            make.right.equalToSuperview().inset(20)
            make.height.equalTo(44)
            make.top.equalToSuperview().offset(5)
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}


/// 底部的按钮
private final class QDLineGraphBottomButton: UIImageView {
 
    /// 显示的标题
    let titleLabel = UILabel()
    /// 显示的小圆点
    let circleView = UIView()
    
    override init(frame: CGRect = .zero) {
        super.init(frame: frame)
        
        isUserInteractionEnabled = true
        image = "metro_bottomButton_background".namedImage
        
        titleLabel.textColor = QDColors.black
        titleLabel.font = Font.medium.font(size: 14)
        
        circleView.backgroundColor = .red
        circleView.layer.cornerRadius = 3
        
        addSubview(titleLabel)
        addSubview(circleView)
        
        circleView.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(23)
            make.top.equalToSuperview().offset(22)
            make.width.height.equalTo(6)
        }
        
        titleLabel.snp.makeConstraints { (make) in
            make.left.equalTo(circleView.snp.right).offset(4)
            make.centerY.equalTo(circleView)
            make.height.equalTo(20)
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    override var intrinsicContentSize: CGSize {
        return CGSize(width: 112, height: 54)
    }
}


/// 线网图底部的站点信息视图
final class QDLineGraphBottomStationView: UIView {
    

    /// 显示站点名字的标签
    let nameLabel = UILabel()
    /// 定位的按钮
    private let localButton = UIButton()
    /// 设置为起点的按钮
    private let startButton = QDLineGraphBottomButton()
    /// 设置为终点的按钮
    private let endButton = QDLineGraphBottomButton()
    /// 站点详情的按钮
    private let detailButton = UIButton()
    /// 下划线
    private let lineView = UIView()
    /// 关闭的按钮
    private let closeButton = UIButton()
    /// 背景图
    private let backgroundView = UIImageView()
    
    /// 响应的类型
    enum ActionType {
        /// 点击关闭
        case close
        /// 点击设置起点
        case start
        /// 点击设置终点
        case end
        /// 点击详情
        case detail
    }
    
    /// 点击事件
    var actionHandler: ((QDLineGraphBottomStationView,ActionType)->())?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        backgroundColor = .clear
        
        //设置属性
        backgroundView.image = "metro_window_background".namedImage?.resizableImage(withCapInsets: UIEdgeInsets(top: 9, left: 9, bottom: 5, right: 0), resizingMode: .stretch)
        
        localButton.backgroundColor = .white
        localButton.adjustsImageWhenHighlighted = false
        localButton.setImage("metro_local".namedImage, for: .normal)
        
        nameLabel.textColor = QDColors.black
        nameLabel.font = Font.medium.font(size: 20)
        nameLabel.text = "太平角公园（一疗）"
        
        detailButton.adjustsImageWhenHighlighted = false
        detailButton.titleEdgeInsets.left = 1
        detailButton.titleLabel?.textAlignment = .right
        detailButton.titleLabel?.font = Font.medium.font(size: 14)
        detailButton.setTitleColor(QDColors.black, for: .normal)
        detailButton.setTitle("站点详情", for: .normal)
        if !isSmallerScreen() {
            detailButton.setImage("metro_details".namedImage, for: .normal)
        }
        detailButton.addTarget(self, action: #selector(actionButtonDidTap(sender:)), for: .touchUpInside)
        
        startButton.titleLabel.text = "设为起点"
        startButton.circleView.backgroundColor = #colorLiteral(red: 0.3215686275, green: 0.8, blue: 0.4156862745, alpha: 1)
        startButton.addUIControlHandlerTarget(self, action: #selector(actionButtonDidTap(sender:)))
        
        endButton.titleLabel.text = "设为终点"
        endButton.circleView.backgroundColor = #colorLiteral(red: 0.9490196078, green: 0.3803921569, blue: 0.3803921569, alpha: 1)
        endButton.addUIControlHandlerTarget(self, action: #selector(actionButtonDidTap(sender:)))

        lineView.backgroundColor = 235.ritl_color
        
        closeButton.adjustsImageWhenHighlighted = false
        closeButton.setImage("metro_close".namedImage, for: .normal)
        closeButton.addTarget(self, action: #selector(actionButtonDidTap(sender:)), for: .touchUpInside)
        
        addSubview(backgroundView)
        addSubview(localButton)
        addSubview(nameLabel)
        addSubview(startButton)
        addSubview(endButton)
        addSubview(detailButton)
        addSubview(lineView)
        addSubview(closeButton)
        
        backgroundView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        localButton.snp.makeConstraints { (make) in
            make.width.height.equalTo(20)
            make.top.equalToSuperview().offset(24.5 + 9)
            make.left.equalToSuperview().offset(16.5)
        }
        
        nameLabel.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(37)
            make.top.equalToSuperview().offset(29)
            make.height.equalTo(28)
        }
        
        lineView.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(20)
            make.right.equalToSuperview().inset(20)
            make.height.equalTo(0.5)
            make.top.equalTo(nameLabel.snp.bottom).offset(16)
        }
        
        closeButton.snp.makeConstraints { (make) in
            make.centerY.equalTo(nameLabel)
            make.right.equalToSuperview().inset(20)
            make.width.height.equalTo(16)
        }
        
        startButton.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(13)
            make.top.equalTo(lineView.snp.bottom).offset(10.5)
            make.width.equalTo(112)
        }
        
        endButton.snp.makeConstraints { (make) in
            make.centerY.equalTo(startButton)
            make.left.equalTo(startButton.snp.right).offset(isSmallerScreen() ? 0 : 6)
            make.width.equalTo(112)
        }
        
        detailButton.snp.makeConstraints { (make) in
            make.right.equalToSuperview().inset(20)
            make.centerY.equalTo(startButton)
            make.height.equalTo(20)
            if !isSmallerScreen() {
                make.width.equalTo(80)
            }else {
                make.left.equalTo(endButton.snp.right)
            }
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    @objc func actionButtonDidTap(sender: UIButton){
        
        var actionType = ActionType.close
        
        switch sender {
        case startButton: actionType = .start
        case endButton: actionType = .end
        case detailButton: actionType = .detail
        default: break
        }
        
        //进行闭包
        actionHandler?(self,actionType)
    }
}
